﻿using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using AppFramework.DataExporting.Excel.NPOI;
using AppFramework.DinosaursParkMes.Material.Dto;
using AppFramework.DinosaursParkMes.MaterialModel;
using AppFramework.DinosaursParkMes.ProductWork.Dto;
using AppFramework.DinosaursParkMes.TicketTask.DTO;
using AppFramework.Dto;
using AppFramework.Storage;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.Codingrule.Exporting
{
    public class MaterialExcelExporter : NpoiExcelExporterBase, IMaterialExcelExporter
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;
        public MaterialExcelExporter(ITempFileCacheManager tempFileCacheManager, ITimeZoneConverter timeZoneConverter, IAbpSession abpSession) : base(tempFileCacheManager)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }

        public FileDto ExportToFile(List<MaterialExcelDto> CodingDtos)
        {
            return CreateExcelPackage(
                "物料信息.xlsx",
                excelPackge =>
                {
                    var sheet = excelPackge.CreateSheet("Material");
                    //生成xlxs头
                    AddHeader(
                        sheet,
                        ("物料编号"),
                        ("物料名称"),
                        ("规格型号"),
                        ("单位"),
                        ("物料类型"),
                         ("物料属性"),
                        ("物料分类"),
                        ("状态"),
                         ("有效期"),
                        ("有效期单位"),
                        ("报警天数"),
                         ("库存上限"),
                          ("库存下限"),
                           ("采购价格"),
                            ("备注")
                        //L("更新时间")
                        );
                    //添加对象
                    AddObjects(
                        sheet, CodingDtos,
                        _ => _.Materialnumber,
                        _ => _.Name,
                        _ => _.Specificationsversion,
                        _ => _.Unit,
                        _ => _.Materialtype,
                        _ => _.MaterialProperty,
                        _ => _.Materialclassify,
                        _ => _.State,
                        _ => _.Periodofvalidity,
                          _ => _.Periodofvalidityunit,
                            _ => _.Alarmdays,
                              _ => _.Inventoryceiling,
                                _ => _.Stockfloor,
                                    _ => _.Purchaseprice,
                                        _ => _.Remark
                        //_=> _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
                        );
                    //for (int i = 0; i < CodingDtos.Count; i++)
                    //{
                    //    SetCellDataFormat(sheet.GetRow(i).Cells[9], "yyyy-MM-dd");
                    //}
                    for (int i = 0; i < 9; i++)
                    {
                        sheet.AutoSizeColumn(i);
                    }
                });
        }

        public FileDto ProductWorkExcelExportToFile(List<ProductWorkExcelDto> CodingDtos)
        {
            return CreateExcelPackage(
                "工单信息.xlsx",
                excelPackge =>
                {
                    var sheet = excelPackge.CreateSheet("ProductWorkModels");
                    //生成xlxs头
                    AddHeader(
                        sheet,
                        ("工单编号"),
                        ("工单名称"),
                        ("工单进度"),
                        ("关联计划"),
                        ("产品名称"),
                         ("产品编号"),
                        ("规格型号"),
                        ("产品类型"),
                         ("单位"),
                        ("需求日期"),
                        ("计划数量"),
                         ("实际生产数量"),
                          ("计划开工时间"),
                           ("计划完工时间"),
                            ("实际开工时间"),
                             ("实际完工时间"),
                              ("状态"),
                            ("备注")

                        //L("更新时间")
                        );
                    //添加对象
                    AddObjects(
                        sheet, CodingDtos,
                        _ => _.WorkOrderNumber,
                        _ => _.WorkOrderName,
                        _ => _.WorkOrderSchedule,
                        _ => _.RelevanceExplain,
                        _ => _.ProductName,
                        _ => _.ProductNumber,
                        _ => _.Specification,
                        _ => _.ProductType,
                        _ => _.Unit,
                        _ => _.DemandDate,
                        _ => _.ExplainNum,
                         _ => _.AccomplishNum,
                         _ => _.ExplainStartDate,
                               _ => _.ExplainEndDate,
                                   _ => _.PracticalStartDate,
                                        _ => _.PracticalEndDate,
                               _ => _.Remark,
                                   _ => _.State
                        //_=> _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
                        );
                    //for (int i = 0; i < CodingDtos.Count; i++)
                    //{
                    //    SetCellDataFormat(sheet.GetRow(i).Cells[9], "yyyy-MM-dd");
                    //}
                    for (int i = 0; i < 9; i++)
                    {
                        sheet.AutoSizeColumn(i);
                    }
                });
        }

        public FileDto TicketTaskFile(List<TicketTaskOutPutDTO> dto)
        {
            return CreateExcelPackage(
                "工单任务.xlsx",
                excelPackge =>
                {
                    var sheet = excelPackge.CreateSheet("TicketTaskOutPutDTO");
                    AddHeader(
                        sheet,
                        ("任务编号"),
                        ("任务名称"),
                        ("站点名称"),
                        ("工单编号"),
                        ("工单名称"),
                        ("工艺流程"),
                        ("工序名称"),
                        ("工序编号"),
                        ("计划数量"),
                        ("实际生产数量"),
                        ("状态")
                        );
                    AddObjects(
                        sheet, dto,
                        _=>_.Missionnumber,
                        _=>_.Taskname,
                        _=>_.Sitename,
                        _=>_.Workordernumber,
                        _=>_.WorkorderName,
                        _=>_.Technologicalprocess,
                        _=>_.Processname,
                        _=>_.Procedurenumber,
                        _=>_.Plannedquantity,
                        _=>_.Realquantity,
                        _=>_.States
                        );
                    for (int i = 0; i < 10; i++)
                    {
                        sheet.AutoSizeColumn(i);
                    }
                }
                );
        }
    }
}
